﻿using MediatR;
using SlaughterItem.Common.Domains.Check_Ins;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Domains.Setup;
using SlaughterItem.Common.Domains.UPCTable;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.Killplan
{
    /// <summary>
    /// 批宰计划显示
    /// </summary>
    public class SlaughterQueryHandler : IRequestHandler<SlaughterQueryCommand, Result>
    {
        private readonly IBaseRepositoryGet<Slaughter> slaughterRepository;
        private readonly IBaseRepositoryGet<CheckIn> checkInRepository;
        private readonly IBaseRepositoryGet<Slaughteredtes> slaughteredRepository;
        private readonly IBaseRepositoryGet<Product> productRepository;
        private readonly IBaseRepositoryGet<Supplier> supplierRepository;
        private readonly IBaseRepositoryGet<Waitings> waitingsRepository;
        private readonly IBaseRepositoryGet<LiveAnimals> liveAnimalsRepository;
        private readonly IBaseRepositoryGet<UPCData> UPCData;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="slaughterRepository"></param>
        /// <param name="checkInRepository"></param>
        /// <param name="slaughteredRepository"></param>
        /// <param name="productRepository"></param>
        ///  <param name="supplierRepository"></param>
        ///  <param name="waitingsRepository"></param>
        public SlaughterQueryHandler(IBaseRepositoryGet<Slaughter> slaughterRepository, IBaseRepositoryGet<CheckIn> checkInRepository, IBaseRepositoryGet<Slaughteredtes> slaughteredRepository, IBaseRepositoryGet<Product> productRepository, IBaseRepositoryGet<Supplier> supplierRepository, IBaseRepositoryGet<Waitings> waitingsRepository,
            IBaseRepositoryGet<LiveAnimals> liveAnimalsRepository, IBaseRepositoryGet<UPCData> UPCData)
        {
            this.slaughterRepository = slaughterRepository;
            this.checkInRepository = checkInRepository;
            this.slaughteredRepository = slaughteredRepository;
            this.productRepository = productRepository;
            this.supplierRepository = supplierRepository;
            this.waitingsRepository = waitingsRepository;
            this.liveAnimalsRepository = liveAnimalsRepository;
            this.UPCData = UPCData;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(SlaughterQueryCommand request, CancellationToken cancellationToken)
        {
             var result= new Result() { Code=(int)CommonErrorCode.Success};

            var list = (from a in slaughterRepository.GetAll()
                        join b in checkInRepository.GetAll()
                        on a.CheckInId equals b.CheckInId
                        join c in liveAnimalsRepository.GetAll()
                        on a.LiveAnimalsId equals c.LiveAnimalsId
                        join d in slaughteredRepository.GetAll()
                        on a.SlaughteredId equals d.SlaughteredId
                        join e in UPCData.GetAll() on d.MarkId equals e.GoodsMinuteId
                        where a.IsDelete == false
                        select new
                        {
                            c.LiveAnimalsDocument,//单据号
                            a.SlaughterId,//批宰编号
                            b.CheckInNo,//进场登记编号
                            b.CheckInBatch,//进场批次
                            a.SlaughterMaxType,//屠宰类型
                            a.SlaughterType,//送宰类型
                            a.SlaughterNum,//计划屠宰头数
                            a.SlaughterDate,//计划屠宰日期
                            a.ProjectDateTime,//计划时间
                            a.Resttime,//静养时间
                            a.SlaughterRemark,//备注
                            a.SlaughterNo,
                            d.MarkId,
                            e.UPCDataName,

                        }).ToList();

            //var list = slaughterRepository.GetAll().Where(x=>x.IsDelete==false).ToList();

            //if (request.SlaughterSDate != null)
            //{
            //    list=list.Where(x=>x.SlaughterDate==request.SlaughterSDate).ToList();
            //}
            if (!string.IsNullOrEmpty(request.LiveAnimalsDocument))
            {
                list = list.Where(x => x.LiveAnimalsDocument.Contains(request.LiveAnimalsDocument)).ToList();
            }

            var totalCount = list.Count;
            var totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
            list = list.OrderBy(x => x.SlaughterId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            var SlaughterData = new
            {
                list,
                totalCount,
                totalPage
            };

            result.Data = SlaughterData;
            result.Msg = "成功";
            return result;
        }
    }
}
